/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import entity.Users;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.ScheduleExpression;
import javax.faces.bean.ManagedBean;
import singleton.SingletonSessionScheduleProcessBeanLocal;
import stateless.StatelessSessionLoginBeanLocal;
import stateless.StatelessSessionSigninBeanLocal;

/**
 *
 * @author antonio
 */
@ManagedBean(name = "controller")
@javax.faces.bean.SessionScoped
public class Controller implements Serializable  {
    
    private Users user;
    private String username, password;
    private boolean logged;
    private String hour, minute;
    private String next;
    @EJB
    private StatelessSessionSigninBeanLocal signinBean;
    @EJB
    private StatelessSessionLoginBeanLocal loginBean;
    @EJB
    private SingletonSessionScheduleProcessBeanLocal ScheduleProcessBean;
  
    /**
     * Creates a new instance of Controller
     */
    public Controller() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLogged(boolean logged) {
        this.logged = logged;
    }
    
    public boolean isLogged() {
        return this.logged;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }
    
    public String signin() {
        user = new Users(username, password);
        if(this.signinBean.signin(user)) {
            this.setLogged(true);
            return "welcome";
        }
        return "signinerror";
    }
    
    public String login() {
        this.user = this.loginBean.login(this.username, this.password);
        if(this.user != null) {
            this.setLogged(true);
            return "welcome";
        }
        return "loginerror";    
    }
    
    public String logout() {
        this.setLogged(false);
        return "index";
    }
    
    public void scheduleProcess() {
        ScheduleExpression expression = new ScheduleExpression();
        expression.hour(this.hour);
        expression.minute(this.minute);
        Date nextDate = this.ScheduleProcessBean.scheduleProcessing(expression, user);
        next = nextDate.toString();
    }
    
}
